package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.testapplication.adapter.RecyclerLinearAdapter;
import com.example.testapplication.bean.Goods;
import com.example.testapplication.widget.SpacesItemDecoration;

public class RecyclerLinearActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_linear);
        initRecyclerLinear();
    }

    private void initRecyclerLinear() {
        // 从布局获取文件中取名为rv_linear的循环图
        RecyclerView rv_linear = findViewById(R.id.rv_linear);
        // 创建一个垂直方向的线性布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
        // 设置循环视图的布局管理器
        rv_linear.setLayoutManager(manager);
        //构建一个公众号列表的线性适配器
        RecyclerLinearAdapter adapter = new RecyclerLinearAdapter(this, Goods.getDefaultList());
        // 点击监听器
        adapter.setOnItemClickListener(new RecyclerLinearAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecyclerLinearActivity.this, Goods.getDefaultGrid().get(position).title, Toast.LENGTH_SHORT).show();
            }
        });
        // 设置线性列表的长按监听器
        //adapter.setOnItemLongClickListener((RecyclerLinearAdapter.OnItemLongClickListener) adapter);
        rv_linear.setAdapter(adapter);
        // 设置默认动画效果
        rv_linear.setItemAnimator(new DefaultItemAnimator());
        // 添加列表项目之间的开白装饰
        rv_linear.addItemDecoration(new SpacesItemDecoration(1));

    }
}